/*
 * Copyright (c) 2009 Tim Mead
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/**
 * Application initialisation command. This is used to setup the initial
 * state of the application.
 */
package com.misoneism.fire.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.misoneism.fire.model.ModelLocator;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.display.Sprite;
	import flash.errors.SQLError;
	import flash.filesystem.File;
	
	import mx.controls.Alert;
	import mx.core.Application;
	
	public class InitCommand
			implements ICommand
	{
		private static const DB_FILE:String = "fireDb";
		
		public function execute(event:CairngormEvent):void
		{
			var ml:ModelLocator = ModelLocator.getInstance();
			
			// Load the database
			var conn:SQLConnection = ml.applicationContext.getObject("sqlConnection");
			var file:File = File.applicationStorageDirectory.resolvePath(DB_FILE);
			
			try
			{
				conn.open(file);
			}
			catch (error:SQLError)
			{
				// TODO: Throw an error to the user
				Alert.show("Error opening Victorian Fire Viewer database.", "Error Loading Victorian Fire Viewer",
					Alert.OK, Application.application as Sprite);
			}
			
			ml.dbConnection = conn;
			
			// Create the tables (if necessary)
			if (conn != null)
			{
				//removeTables(conn);
				createTables(conn);
			}
			
			ml.mapReady = false;
			
			// Setup the default status message
			ml.statusMessage = "";
		}
		
		/**
		 * Clean up the database, removing any tables that may have been
		 * created.
		 */
		protected function removeTables(conn:SQLConnection):void
		{
			var sql:String =
				"DROP TABLE geocode";
				
			var statement:SQLStatement = new SQLStatement();
			statement.sqlConnection = conn;
			statement.text = sql;
			statement.execute();
		}

		/**
		 * If necessary, create the database tables used by this application.
		 */
		protected function createTables(conn:SQLConnection):void
		{
			var sql:String =
				"CREATE TABLE IF NOT EXISTS geocode(" + 
				"  id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
				"  address STRING, " + 
				"  latitude NUMBER, " + 
				"  longitude NUMBER, " + 
				"  create_date DATE " + 
				")";
				
			var statement:SQLStatement = new SQLStatement();
			statement.sqlConnection = conn;
			statement.text = sql;
			statement.execute();
		}
	}
}